/*
 * Demo.java
 *
 * www.bouthier.net
 * 2001
 */

package ht_birds;

import hypertree.HyperTree;

import hypertree.SwingHTView;

import java.io.*;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import javax.swing.JFrame;

import ui.mainWindow;

import dao.BirdDAO;
import dao.BirdVO;
import dao.ConnectionManager;



/**
 * The Demo class implements a demo for HyperTree. It display an HyperTree
 * view of a demo tree.
 * The demo tree is build from a file tree, passed in parameter.
 * Demo could take an argument, the path from which 
 * start the representing of files.
 * If no arguments is given, the treemap start from the root.
 */
public class Demo {

    private static HTFileNode root      = null; // the root of the demo tree
    private static HyperTree  hypertree = null; // the hypertree builded

    
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }

    /**
     * Display a demo HyperTree.
     * @throws SQLException 
     */
    public static void main(String[] args) throws SQLException {
        String pathRoot = null;

        if (args.length > 0) {
            pathRoot = args[0];
        } else {
            pathRoot = "birds";
        }
        
        
        try{
			ConnectionManager.connect(mainWindow.BD_NAME);
		}
		catch(SQLException ex){
			System.out.println("Error while connecting to db: " +ex);
		}
		catch(ClassNotFoundException ex){
			System.out.println("Class not found, check classpath");
		}      
        
        
       Vector<BirdVO> b = BirdDAO.arbitrarySQL("select * from bird");
       
       try {
		ConnectionManager.disconnect();
       } catch (SQLException e) {
		e.printStackTrace();
       }
       
       
      
       for(int i=0; i<b.size();i++)
       {
    	  String name = b.get(i).getScientific_name();
    	   //System.out.println(name);
    	   new File ("birds").mkdir();
    	   String dirs = "birds\\" + name.substring(0, 1) + "\\" + name.split(" ")[0] ;
    	   new File (dirs).mkdirs();
    	   try {
			new File (dirs+"\\"+name.split(" ")[1]).createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       }
        
       File rootFile = new File(pathRoot);
        try {
            System.out.println("Starting the hyperbolic tree from " +
                                rootFile.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        if (! rootFile.exists()) {
            System.out.println("Can't start hypertree : " + rootFile.getName() +
                               " does not exist.");
            return;
        }


        root = new HTFileNode(rootFile);
        if (root == null) {
            System.err.println("Error : can't start hypertree from " +
                                rootFile.getAbsolutePath());
            return;
        }

     
        
        
         hypertree = new HyperTree(root);
        SwingHTView view = hypertree.getView();
	//HTNode firstchild = (HTNode) root.children().next();
	//view.translateToOrigin(firstchild);
        JFrame viewFrame = new JFrame(root.getName());
        viewFrame.setContentPane(view);
        viewFrame.setSize(500, 500);
        //viewFrame.pack();
        viewFrame.setVisible(true);        
        
        delete(new File("birds"));    
        
        
        
        
    }
    
    
    static void delete(File f){
    	  if (f.isDirectory()) {
    	    for (File c : f.listFiles())
    	      delete(c);
    	  }
    	  if (!f.delete())
    	    System.out.println("Failed to delete file: " + f);
    	    		
    	//delete(f);
    	}
    	

}

